import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.Time;

/**
 * 运算后的集合
 * 也用于将数据导入数据库
 */
public class MySet {
    public String id;
    public int specialCount;//突然去的食堂编号
    public Date specialCanteenD=new Date(0);//突然去食堂的日期
    public int specialCanteen=99;//突然去的食堂编号
    public int breakfastA8=0,special_shop_name; //8点前早饭次数--    ////
    public Time earliestTime=new Time(-8*1000*3600);//最早早饭时间
    public Date earliestDate=new Date(0);
    public Time latestTime=new Time(-8*1000*3600); //最晚晚饭时刻
    public int latestLoc=99; //最晚晚饭食堂编号
    public int[] regularTime;//6 7 8 9点的早饭次数（键值0-3）
    public int sum=0;//早饭总天数
    public int serialSum=0;//连续早饭总天数
    public int[] dinnerTime;//一般晚餐时间 16-22
    public int[] mealSum;//各食堂次数（键值0-14
    public int printSum=0;//打印店次数
    public int[][] marketSum;//各超市次数 其中0为总数 1-7为上午 下午 晚上 详见函数timeS
    public double[] marketSpend;//各超市消费总额 5
    Date specialShopD=new Date(0);
    double specialShopA=0;
    public void test(Connection connection) throws Exception{//消费最多一笔的超市名
        String sql="UPDATE fuckndzd_test2 "+
                "SET special_shop_name = ?,is_done = 1 "+
                " WHERE stu_id='"+id+"'";
        PreparedStatement pS;
        pS=connection.prepareStatement(sql);
        String marketS="";
        int ti=special_shop_name;
        if(ti==0)marketS="华大超市";
        else if(ti==1)marketS="佳炜食杂店";
        else if(ti==2)marketS="1314便利店";
        else if(ti==3)marketS="可思汉超市";
        else if(ti==4)marketS="见福便利店";
        else marketS="某不存在的超市";
        pS.setString(1,marketS);
        pS.execute();

    }
    public void FBI(Connection connection){//其他字段
        try {
            String sql="UPDATE fuckndzd_test2 "+
                    "SET amount_favourite_canteen=? ,count_breakfast=? ,count_without_breakfast=? ,"+//3
                    "breakfast_time=? ,earliest_breakfast_time=? ,earliest_breakfast_date=?,average_dinner_time=? ,last_dinner_time=? , "+
                    "last_dinner_canteen=?,special_dinner_time=?,special_dinner_canteen=?,amount_eat_count=?,"+//9-12
                    "amount_print_count=?,amount_shop=?,amount_shop_post=?,favourite_shop=?,favourite_shop_time=?,sum_count=?, "+
                    "is_done=1,breakfast_after_8=?,special_shop_time=?,special_shop_amount=?  "+
                    " WHERE stu_id='"+id+"'";
//            String sql2="UPDATE fuckndzd_test2 "+
//                    "SET amount_favourite_canteen=NULL ,count_breakfast=NULL ,count_without_breakfast=NULL ,"+//3
//                    "breakfast_time=NULL ,earliest_breakfast_time=NULL ,earliest_breakfast_date=NULL,average_dinner_time=NULL ,last_dinner_time=NULL , "+
//                    "last_dinner_canteen=NULL,special_dinner_time=NULL,special_dinner_canteen=NULL,amount_eat_count=NULL,"+//9-12
//                    "amount_print_count=NULL,amount_shop=NULL,amount_shop_post=NULL,favourite_shop=NULL,favourite_shop_time=NULL,sum_count=NULL, "+
//                    "is_done=0,breakfast_after_8=NULL,user_view.fuckndzd_test2.special_shop_time=NULL,user_view.fuckndzd_test2.special_shop_amount=NULL  ";
//            PreparedStatement pStatement;
//            pStatement=connection.prepareStatement(sql2);
//            pStatement.execute();

            PreparedStatement pS;
            pS=connection.prepareStatement(sql);
            pS.setInt(19,breakfastA8);
            pS.setDate(20,specialShopD);
            pS.setDouble(21,specialShopA/100);

            String tmp;int ti=99;int MaxTi=0;int sumMeal=mealSum[0];
            for(int i=0;i<=14;i++){
                if(mealSum[i]>MaxTi){
                    ti=i;
                    MaxTi=mealSum[i];
                }
                sumMeal+=mealSum[i];
            }
            tmp=locS(ti);
            pS.setString(1,tmp);
            pS.setInt(2,sum);
//            print(tmp);//
//            print(sum);
            /**
             * kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
             */
            if(id.length()>7) {
                if (id.substring(0, 2).compareTo("19") == 0) pS.setInt(3, 127 - sum);
                else pS.setInt(3, 281 - sum);
            }
            else pS.setInt(3, 281 - sum);
            ti=-6;MaxTi=0;
            for(int i=0;i<4;i++){
                if(regularTime[i]>MaxTi){
                    ti=i;
                    MaxTi=regularTime[i];
                }
            }
            pS.setInt(4,ti+6);
            pS.setTime(5,earliestTime);
            pS.setDate(6,earliestDate);
//            print(ti+6);
//            print(earliestTime);
//            print(earliestDate);
            ti=-16;MaxTi=0;
            for(int i=0;i<7;i++){
                if(dinnerTime[i]>MaxTi){
                    ti=i;
                    MaxTi=dinnerTime[i];
                }
            }
            pS.setInt(7,ti+16);
            pS.setTime(8,latestTime);
            pS.setString(9,locS(latestLoc));
            pS.setDate(10,specialCanteenD);
            pS.setString(11,locS(specialCanteen));
            pS.setInt(12,sumMeal);
            pS.setInt(13,printSum);
//            print(ti+16);
//            print(latestTime);
//            print(locS(latestLoc));
//            print(specialCanteenD);
//            print(locS(specialCanteen));
//            print(sumMeal);
//            print(printSum);
            ti=-1;MaxTi=0;int sumMarket=marketSum[0][0];
            for(int i=0;i<marketSum.length;i++){
                if(marketSum[i][0]>MaxTi){
                    ti=i;
                    MaxTi=marketSum[i][0];
                }
                sumMarket+=marketSum[i][0];
            }
            if(ti>-1) {
                pS.setDouble(14, marketSpend[ti] / 100);
                pS.setInt(15,marketSum[ti][0]);
            }
            else {
                pS.setDouble(14,0);
                pS.setInt(15,0);
            }

//            print(marketSpend[ti]/100);
//            print(marketSum[ti][0]);
            String marketS;
            if(ti==0)marketS="华大超市";
            else if(ti==1)marketS="佳炜食杂店";
            else if(ti==2)marketS="1314便利店";
            else if(ti==3)marketS="可思汉超市";
            else if(ti==4)marketS="见福便利店";
            else marketS="某不存在的超市";
            pS.setString(16,marketS);
//            print(marketS);
            if(ti>-1) {
                int t2 = -1;
                int maxT2 = 0;
                for (int i = 1; i < 8; i++) {
                    if (marketSum[ti][i] > maxT2) {
                        t2 = i;
                        maxT2 = marketSum[ti][i];
                    }
                }
                pS.setString(17, timeS(t2));
            }
            else {
                pS.setString(17, timeS(-1));
            }
            pS.setInt(18, sumMeal + printSum + sumMarket);
//            print(timeS(t2));
//            print(sumMarket+printSum+sumMeal);

            pS.execute();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    static String timeS(int k){//查找时间段字样
        String tmp="";

        switch (k){
            case -1:
                tmp="不存在的时间";
                break;
            case 1:
                tmp="清晨";
                break;
            case 2:
                tmp="上午";
                break;
            case 3:
                tmp="中午";
                break;
            case 4:
                tmp="下午";
                break;
            case 5:
                tmp="傍晚";
                break;
            case 6:
                tmp="晚上";
                break;
            case 7:
                tmp="深夜";
                break;
        }
        return tmp;
    }
    static String locS(int k){//
        String tmp;

        switch (k){
            case 0:
                tmp="华文餐厅一楼";
                break;
            case 1:
                tmp="华文餐厅二楼";
                break;
            case 2:
                tmp="第一食堂";
                break;
            case 3:
                tmp="第二食堂";
                break;
            case 4:
                tmp="第三食堂";
                break;
            case 5:
                tmp="西苑食堂";
                break;
            case 6:
                tmp="南苑食堂";
                break;
            case 7:
                tmp="清真餐厅";
                break;
            case 8:
                tmp="凤竹餐厅";
                break;
            case 9:
                tmp="凤林餐厅";
                break;
            case 10:
                tmp="凤华餐厅";
                break;
            case 11:
                tmp="凤翔餐厅";
                break;
            case 12:
                tmp="紫荆一食堂";
                break;
            case 13:
                tmp="紫荆二食堂";
                break;
            case 14:
                tmp="紫荆三食堂";
                break;
            default:
                tmp="某不存在餐厅";
                break;
        }
        return tmp;
    }

    <T> void print(T a){
        System.out.print(a+" ");
    }
    public void printAll(){
        Date date=new Date(earliestDate.getTime()+earliestTime.getTime());
        print("最早早饭："+earliestDate+" "+earliestTime+" "+"早饭总天数 "+sum+" 连续天数 "+serialSum+" 一般时间 ");
        int tmp=0;
        for(int i=0;i<4;i++){
            if(regularTime[i]>regularTime[tmp])tmp=i;
            print(regularTime[i]+" ");
        }
        print("--"+(tmp+6)+"点\n");
        print("打印店："+printSum+"\n");
        print("晚餐时间：");
        for(int i=0;i<7;i++){
            print(dinnerTime[i]+" ");
        }
        print("最晚晚饭时刻 食堂："+latestTime+" "+latestLoc+"\n");
        print("突然去食堂："+specialCanteenD+" "+specialCanteen+"("+specialCount+"天)\n");
        print("\n华文餐厅：（1楼 2楼）：");
        for(int i=0;i<=1;i++){
            print(mealSum[i]+" ");
        }
        print("\n泉州：（第1 2 3餐厅 西苑 南苑 清真餐厅）：");
        for(int i=2;i<=7;i++){
            print(mealSum[i]+" ");
        }
        //print(mealSum[20]+" ");
        print("\n厦门：（凤竹 林 华 翔餐厅 紫1 2 3餐厅）：");
        for(int i=8;i<=14;i++){
            print(mealSum[i]+" ");
        }
        int max=0;
        for(int i=0;i<marketSum.length;i++){
            if(marketSum[i][0]>marketSum[max][0])max=i;
        }
        String marketS="";
        if(max==0)marketS="华大超市";
        else if(max==1)marketS="佳炜食杂店";
        else if(max==2)marketS="1314便利店";
        else if(max==3)marketS="可思汉超市";
        else if(max==4)marketS="见福便利店";
        print("\n最常去的超市："+marketS+" 总次数："+marketSum[max][0]+" 消费总额："+marketSpend[max]/100+" 各时段次数（清晨 上午 中午 下午 傍晚 晚上 深夜）：");
        for(int i=1;i<8;i++){
            print(marketSum[max][i]+" ");
        }
    }
}
